import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { observable } from 'rxjs';
import { Organization, OrganizationRelation, OrganizationTypeEnum, PageableResponse } from '../domain/sparrow';

@Injectable({
  providedIn: 'root'
})
export class OrganizationService {


  host:string = "http://localhost:8091/api";
  // api!: DefaultService;
  constructor(private httpClient: HttpClient) {
    // this.api = new DefaultService(httpClient, this.host, new Configuration())
   }

  public getTreeByParentId(parentId: string){
    return this.httpClient.get<any>(this.host+"/orgranizations/getTreeByParentId",{ params:{parentId:parentId}})
  }

  public saveBatch(organizations: Organization[]){
    return this.httpClient.post(this.host+"/organizations/batch",organizations);
  }

  public save(organization: Organization){
    if(organization.id === undefined)
      return this.httpClient.post<Organization>(this.host+"/organizations",organization);
    else
      return this.httpClient.patch<Organization>(this.host+"/organizations/" + organization.id,organization);
  }

  addRelation(organizationRelations: OrganizationRelation[]){
    return this.httpClient.post<any>(this.host+"/organizations/addRelation",organizationRelations);
  }

  updateParent(organizationRelations: OrganizationRelation[]){
    return this.httpClient.delete<any>(this.host+"/organizations/updateParent",{body:organizationRelations});
  }

  public saveRelations(organizationRelations: OrganizationRelation[]){
    return this.httpClient.post(this.host+"/organizations/relations/batch",organizationRelations);
  }

  public deleteBatch(ids: string[]){
    return this.httpClient.delete(this.host+"/organizations/batch",{body: ids});
  }

  public getModelName(){
    return this.httpClient.get<any>(this.host+"/organizations/getModelName");
  }

  public getChildren(parentId: string){
    return this.httpClient.get<any[]>(this.host + "/organizations/getChildren",{params:{parentId:parentId}})
    // return this.api.getChildren1(parentId, "ORGANIZATION", undefined);
  }

  public getParents(parentId: string){
    return this.httpClient.get<Organization[]>(this.host + "/organizations/getParents",{params:{parentId:parentId}})
  }


  public getRoles(parentId: string){
    return this.httpClient.get<any[]>(this.host + "/organizations/getRoles",{params:{organizationId:parentId}})
  }

  public getLevels(parentId: string){
    return this.httpClient.get<any[]>(this.host + "/organizations/getLevels",{params:{organizationId:parentId}})
  }

  public getGroups(parentId: string){
    return this.httpClient.get<any[]>(this.host + "/organizations/getGroups",{params:{organizationId:parentId}})
  }

  public getEmployees(parentId: string){
    return this.httpClient.get<any[]>(this.host + "/organizations/getEmployees",{params:{organizationId:parentId}})
  }

  public getRoleChildren(organizationId:string,roleId: string){
    return this.httpClient.get<any[]>(this.host+"/roles/getChildren",{ params:{organizationId:organizationId,roleId:roleId}})
  }

  public getEmployeeChildren(employeeId: string) {
    return this.httpClient.get<any[]>(this.host+"/employees/getChildren",{ params:{parentId:employeeId}})
  }

  public getLevelChildren(organizationId:string,roleId: string){
    return this.httpClient.get<any[]>(this.host+"/levels/getChildren",{ params:{organizationId:organizationId,positionLevelId:roleId}})
  }
}
